Repository navigation
feat(🎞️): createNativeBufferFromPointer takes a colorSpace - #642
Conversation
`RNWebGPU.createNativeBufferFromPointer(pointer, { colorSpace })`: a WebCodecs
`VideoColorSpaceInit` (a `VideoFrame`'s `colorSpace` fits) that says how to
read a YUV buffer carrying no color information of its own, such as an
`AHardwareBuffer` from a MediaCodec `ImageReader`. Without it, BT.709 video
was read with the BT.601 matrix on Android ((226, 0, 31) for red). The fields
it sets replace what the buffer says; a `CVPixelBuffer` keeps its own
attachments otherwise.
…mPointer Wraps the NV12 test frame's CVPixelBufferRef (BT.709 limited range attachments) with createNativeBufferFromPointer and samples it through importExternalTexture: without options and with an empty colorSpace the attachments apply; matrix "bt709" / "smpte170m", fullRange true / false and matrix + fullRange each give the colors a reference YCbCr -> sRGB conversion predicts, and the overrides visibly move the result. Skipped where the device has no IOSurface import (Android reads AHardwareBuffers through Vulkan's YCbCr sampler, which the expectations do not model). JS has no CVPixelBufferRef to hand over (NativeBuffer.handle is the IOSurface), so a test-only RNWebGPU.createTestPixelBuffer, left out of the TypeScript declarations, returns the test frame with that pointer as its handle.
wcandillon
left a comment
There was a problem hiding this comment.
please remove createTestPixelBuffer and use the existing ColorSpace type, don't create a new one.
…Pointer The colorSpace option is now the NativeBufferColorSpace that importExternalTexture's sourceColorSpace takes, read by the same parser, instead of a new WebCodecs-shaped type. createTestPixelBuffer and the e2e spec that needed it are gone.
…sourceColorSpace Wraps the AHardwareBuffer of the NV12 test frame with and without the option and checks the result against importExternalTexture's sourceColorSpace for the same members. Android only: on Apple platforms no JS API gives a CVPixelBufferRef to wrap.
|
Done, thanks. createTestPixelBuffer is gone, and the colorSpace option of createNativeBufferFromPointer now takes NativeBufferColorSpace, parsed by the same code as sourceColorSpace in importExternalTexture. |
…pace Wraps the test frame's AHardwareBuffer with its real color space (BT.709 narrow) and expects pure red and green, the same as sourceColorSpace gives. One submit and one readback for the three draws, and no loose typing.
…ecks The three imports are named (withoutColorSpace, withColorSpace, withSourceColorSpace), the color space is the test frame's own, and a color mismatch shows both pixels.
|
I reworked the spec so it checks the actual bug. The test frame is encoded BT.709 narrow, but its AHardwareBuffer carries no color metadata, so wrapped without a colorSpace it gets the platform's guess. On the emulator that's BT.601, and pure red comes out as 228,0,32. Wrapped with { matrix: "rec709", range: "narrow" }, it comes out pure red and green, the same as sourceColorSpace gives. It fails without the change and passes with it. |
| // how to read a YUV buffer that carries no color information of its own, as | ||
| // an AHardwareBuffer from a MediaCodec ImageReader. The members it sets | ||
| // replace what the buffer says. | ||
| jsi::Value createNativeBufferFromPointer(jsi::Runtime &runtime, |
There was a problem hiding this comment.
may I ask why it's back to the vanilla jsi::Value createNativeBufferFromPointer(jsi::Runtime &runtime, const jsi::Value &, ... signature? Sorry if I missed the obvious reason why?
There was a problem hiding this comment.
No good reason, I copied the shape of createNativeBuffer next to it, which reads its options object by hand. I switched it back to a typed signature: the options are a small descriptor with its own JSIConverter, and its colorSpace is parsed by the same code as sourceColorSpace.
The options are a CreateNativeBufferFromPointerOptions descriptor with its own JSIConverter, whose colorSpace is parsed like sourceColorSpace, instead of reading the raw jsi::Value arguments by hand.
…o feat/native-buffer-colorspace
wcandillon
left a comment
There was a problem hiding this comment.
This is great sorry I still have two small requests (but otherwise, it's good to go for me):
apps/docs/content/api/native-buffer.mdx: update the documentation
CreateNativeBufferFromPointerOptions.h:
@@ -30,7 +30,7 @@ struct JSIConverter<std::shared_ptr<CreateNativeBufferFromPointerOptions>> {
auto prop = arg.getObject(runtime).getProperty(runtime, "colorSpace");
if (prop.isObject()) {
result->colorSpace = external_texture_color_space::parse(
- runtime, prop.getObject(runtime));
+ runtime, prop.getObject(runtime), "colorSpace");
}
}
return result;
add a owner parameter to parse where owner can be sourceColorSpace or colorSpace.
@@ -51,7 +51,8 @@ namespace external_texture_color_space {
-inline wgpu::ColorSpaceYCbCrMatrixDawn parseMatrix(const std::string &name) {
+inline wgpu::ColorSpaceYCbCrMatrixDawn parseMatrix(const std::string &owner,
+ const std::string &name) {
if (name == "identity") {
@@ -64,12 +65,13 @@
- throw std::runtime_error("sourceColorSpace.matrix: unknown value '" + name +
+ throw std::runtime_error(owner + ".matrix: unknown value '" + name +
"' (expected 'identity', 'rec601', 'rec709' or "
"'rec2020')");
}
-inline wgpu::ColorSpaceYCbCrRangeDawn parseRange(const std::string &name) {
+inline wgpu::ColorSpaceYCbCrRangeDawn parseRange(const std::string &owner,
+ const std::string &name) {
@@ -79,11 +81,12 @@
- throw std::runtime_error("sourceColorSpace.range: unknown value '" + name +
+ throw std::runtime_error(owner + ".range: unknown value '" + name +
"' (expected 'identity', 'narrow' or 'full')");
}
-inline wgpu::ColorSpacePrimariesDawn parsePrimaries(const std::string &name) {
+inline wgpu::ColorSpacePrimariesDawn parsePrimaries(const std::string &owner,
+ const std::string &name) {
@@ -99,13 +102,13 @@
- throw std::runtime_error("sourceColorSpace.primaries: unknown value '" +
- name +
+ throw std::runtime_error(owner + ".primaries: unknown value '" + name +
"' (expected 'srgb', 'rec709', 'rec601', "
"'rec2020' or 'display-p3')");
}
-inline wgpu::ColorSpaceTransferDawn parseTransfer(const std::string &name) {
+inline wgpu::ColorSpaceTransferDawn parseTransfer(const std::string &owner,
+ const std::string &name) {
@@ -127,13 +130,16 @@
- throw std::runtime_error("sourceColorSpace.transfer: unknown value '" + name +
+ throw std::runtime_error(owner + ".transfer: unknown value '" + name +
"' (expected 'identity', 'srgb', 'display-p3', "
"'smpte-170m', 'hlg', 'pq' or 'bt-1886')");
}
+// `owner` names the member being parsed in the error messages
+// ("sourceColorSpace", "colorSpace").
inline VideoFrameColorSpace parse(jsi::Runtime &runtime,
- const jsi::Object &value) {
+ const jsi::Object &value,
+ const std::string &owner) {
VideoFrameColorSpace colorSpace;
@@ -146,16 +152,16 @@
if (auto matrix = member("matrix")) {
- colorSpace.matrix = parseMatrix(*matrix);
+ colorSpace.matrix = parseMatrix(owner, *matrix);
}
if (auto range = member("range")) {
- colorSpace.range = parseRange(*range);
+ colorSpace.range = parseRange(owner, *range);
}
if (auto primaries = member("primaries")) {
- colorSpace.primaries = parsePrimaries(*primaries);
+ colorSpace.primaries = parsePrimaries(owner, *primaries);
}
if (auto transfer = member("transfer")) {
- colorSpace.transfer = parseTransfer(*transfer);
+ colorSpace.transfer = parseTransfer(owner, *transfer);
}
return colorSpace;
}
@@ -204,7 +210,7 @@ struct JSIConverter<std::shared_ptr<rnwgpu::GPUExternalTextureDescriptor>> {
result->sourceColorSpace = external_texture_color_space::parse(
- runtime, prop.getObject(runtime));
+ runtime, prop.getObject(runtime), "sourceColorSpace");
…nd document the option external_texture_color_space::parse takes the member it parses, so a bad value passed to createNativeBufferFromPointer says colorSpace.matrix rather than sourceColorSpace.matrix. The API docs show the options and when to pass a colorSpace.
…o feat/native-buffer-colorspace
|
@wcandillon no worries at all. Updated. |
|
🎉 This PR is included in version 0.26.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
Description
I ran into this while building react-native-webcodecs, a WebCodecs implementation for React Native that works alongside react-native-webgpu. The two share native buffers: decoded video frames go to WebGPU through
createNativeBufferFromPointer.On Android, the
AHardwareBuffers that MediaCodec hands out carry no color information. BT.709 video was read with the BT.601 matrix, and the colors came out visibly off.As I understand it, the decoder knows the color space, since it's on the
VideoFrame, but nothing passed it down to the YCbCr conversion. This PR lets the caller pass it.On iOS,
CVPixelBuffers already have their own attachments, so the option only overrides the fields you set.Agent-written content
RNWebGPU.createNativeBufferFromPointer(pointer, { colorSpace })takes a WebCodecsVideoColorSpaceInit(aVideoFrame'scolorSpacefits as is) that says how to read a YUV buffer that carries no color information of its own, such as anAHardwareBufferfrom a MediaCodecImageReader. Without it, BT.709 video was read with the BT.601 matrix on Android ((226, 0, 31) for pure red). The fields it sets replace what the buffer says; aCVPixelBufferkeeps its own attachments otherwise.Tests
NativeBufferColorSpace.spec.ts(7 cases) wraps the NV12 test frame'sCVPixelBufferRef(BT.709 limited-range attachments) withcreateNativeBufferFromPointerand samples it throughimportExternalTexture:colorSpacekeep the buffer's attachments;bt709/smpte170m,fullRangetrue / false, and matrix +fullRangeeach give the colors a reference YCbCr → sRGB conversion predicts, and the overrides visibly move the result.JS has no
CVPixelBufferRefto hand over (NativeBuffer.handleis the IOSurface), so a test-onlyRNWebGPU.createTestPixelBuffer, left out of the TypeScript declarations, returns the test frame with that pointer as its handle. The spec is skipped where the device has no IOSurface import: Android readsAHardwareBuffers through Vulkan's YCbCr sampler, which the expectations do not model.Verified: the spec passes on the iOS 27.2 Simulator (8/8 together with
ExternalTextureColorSpace). The spec was not run on Android.On Android
A BT.709 limited-range video of 100% color bars, decoded by MediaCodec (
frame.colorSpaceis{"primaries":"bt709","transfer":"bt709","matrix":"bt709","fullRange":false}), with the frame imported throughcreateNativeBufferFromPointeronce without options and once with{ colorSpace: frame.colorSpace.toJSON() }, then copied withcopyExternalImageToTextureand read back (Android emulator, arm64):frame.colorSpace